package com.sdry.web.controller.zc.quality;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.llm.User;
import com.sdry.model.zc.ZcLeakHuntingCriterionEntity;
import com.sdry.model.zc.ZcNumberedEntity;
import com.sdry.model.zc.ZcProductInspectionEntity;
import com.sdry.model.zc.ZcProductTestEntity;
import com.sdry.service.zc.ZcCriterionService;
import com.sdry.service.zc.ZcNumberedService;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName:    ZcNumberedController   
 * @Description:  数点
 * @Author:       zc   
 * @CreateDate:   2018年10月9日 下午2:39:53   
 * @Version:      v1.0
 */
@Controller
@RequestMapping("numbered")
public class ZcNumberedController {

	@Resource
	private ZcNumberedService zcNumberedService;
	@Resource
	private ZcCriterionService zcCriterionService;
	/**
	 * 进入数点页面
	 * @return
	 */
	@RequestMapping("enterNumberedPage")
	public String enterNumberedPage(Model model){
		ZcLeakHuntingCriterionEntity zcLeakHuntingCriterionEntity = zcCriterionService.selectLeakHuntingCriterion();
		model.addAttribute("zcLeakHuntingCriterionEntity", zcLeakHuntingCriterionEntity);
		return "/zc/quality/numbered";
	}
	/**
	 * 查询产品报检单列表
	 * @param request
	 * @param response
	 */
	@RequestMapping("selectProductInspectionList")
	public void selectProductInspectionList(HttpServletRequest request,HttpServletResponse response){
		List<ZcProductInspectionEntity> productInspectionList = zcNumberedService.selectProductInspectionList();
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(productInspectionList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过报检单号查询报检单信息
	 * @param request
	 * @param response
	 * @param keyword
	 */
	@RequestMapping("selectProductInspection")
	public void selectProductInspection(HttpServletRequest request,HttpServletResponse response,String keyword){
		if(keyword==null){
			keyword = "";
		}
		ZcProductInspectionEntity zcProductInspectionEntity = zcNumberedService.selectProductInspection(keyword);
		JSONObject json= JSONObject.fromObject(zcProductInspectionEntity);  
		try {
			if(zcProductInspectionEntity!=null){
				ResponseUtil.write(response, json);
			}else{
				ResponseUtil.write(response, 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 保存产品质检单信息
	 * @param request
	 * @param response
	 * @param zcProductTestEntity
	 */
	@RequestMapping("insertProductTest")
	public void insertProductTest(HttpServletRequest request,HttpServletResponse response,ZcProductTestEntity zcProductTestEntity){
		int affact = zcNumberedService.insertProductTest(zcProductTestEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 保存数点结果
	 * @param request
	 * @param response
	 * @param zcNumberedEntity
	 */
	@RequestMapping("insertNumbered")
	public void insertNumbered(HttpServletRequest request,HttpServletResponse response,ZcNumberedEntity zcNumberedEntity,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcNumberedEntity.setTestPerson(userName);
		int affact = zcNumberedService.insertNumbered(zcNumberedEntity);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过组件条码查询质检单信息
	 * @param request
	 * @param response
	 * @param code
	 */
	@RequestMapping("selectProductTest")
	public void selectProductTest(HttpServletRequest request,HttpServletResponse response,String code){
		if(code==null){
			code = "";
		}
		ZcProductTestEntity zcProductTestEntity = zcNumberedService.selectProductTest(code);
		JSONObject json= JSONObject.fromObject(zcProductTestEntity);  
		try {
			if(zcProductTestEntity!=null){
				ResponseUtil.write(response, json);
			}else{
				ResponseUtil.write(response, 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查询这个产品是否已经数点
	 * @param request
	 * @param response
	 * @param ptCode
	 */
	@RequestMapping("countNumberByCode")
	public void countNumberByCode(HttpServletRequest request,HttpServletResponse response,String ptCode){
		int count = zcNumberedService.countNumberByCode(ptCode);
		try {
			ResponseUtil.write(response, count);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 计算报检单已数点产品
	 * @param request
	 * @param response
	 * @param piNumber
	 */
	@RequestMapping("countProductTestByPiNumber")
	public void countProductTestByPiNumber(HttpServletRequest request,HttpServletResponse response,String piNumber){
		int count = zcNumberedService.countProductTestByPiNumber(piNumber);
		try {
			ResponseUtil.write(response, count);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 解绑台车
	 * @param request
	 * @param response
	 * @param piNumber
	 */
	@RequestMapping("unbundle")
	public void unbundle(HttpServletRequest request,HttpServletResponse response,String piNumber){
		int affact = zcNumberedService.unbundle(piNumber);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
